{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create Networks - Advanced"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This tutorial shows how to create a more complex pandapower network step by step. The network includes every element which is availiable in the pandapower framework.\n",
    "\n",
    "The final network looks like this:\n",
    "\n",
    "<img src=\"pics/example_network.png\" width=\"50%\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The structural information about this network are stored in csv tables in the example_advanced folder.\n",
    "\n",
    "For a better overview the creation of the individual components is divided in three steps. Each step handles one of the three voltage levels: high, medium and low voltage. We star by initializing an empty pandapower network:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "from pandapower.create import (\n",
    "    create_empty_network,\n",
    "    create_ext_grid,\n",
    "    create_bus,\n",
    "    create_line,\n",
    "    create_transformer_from_parameters,\n",
    "    create_transformer3w_from_parameters,\n",
    "    create_switch,\n",
    "    create_gen,\n",
    "    create_load,\n",
    "    create_sgen,\n",
    "    create_shunt,\n",
    "    create_xward,\n",
    "    create_impedance\n",
    ")\n",
    "from pandapower.run import runpp\n",
    "from pandapower.toolbox.element_selection import get_element_index\n",
    "import warnings\n",
    "warnings.simplefilter(action='ignore', category=FutureWarning)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#create an empty network\n",
    "net = create_empty_network()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## High voltage level"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Buses\n",
    "\n",
    "<img src=\"pics/example_network_buses_hv_detail.png\" width=\"50%\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "There are two 380 kV and five 110 kV busbars (type=\"b\"). The 380/110 kV substation is modeled in detail with all nodes and switches, which is why we need additional nodes (type=\"b\") to connect the switches."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:22.788537Z",
     "start_time": "2025-10-17T09:01:22.718523Z"
    }
   },
   "outputs": [],
   "source": [
    "# Double busbar\n",
    "create_bus(net, name='Double Busbar 1', vn_kv=380, type='b')\n",
    "create_bus(net, name='Double Busbar 2', vn_kv=380, type='b')\n",
    "for i in range(10):\n",
    "    create_bus(net, name='Bus DB T%s' % i, vn_kv=380, type='n')\n",
    "for i in range(1, 5):\n",
    "    create_bus(net, name='Bus DB %s' % i, vn_kv=380, type='n')\n",
    "\n",
    "# Single busbar\n",
    "create_bus(net, name='Single Busbar', vn_kv=110, type='b')\n",
    "for i in range(1, 6):\n",
    "    create_bus(net, name='Bus SB %s' % i, vn_kv=110, type='n')\n",
    "for i in range(1, 6):\n",
    "    for j in [1, 2]:\n",
    "        create_bus(net, name='Bus SB T%s.%s' % (i, j), vn_kv=110, type='n')\n",
    "\n",
    "# Remaining buses\n",
    "for i in range(1, 5):\n",
    "    create_bus(net, name=f'Bus HV{i}', vn_kv=110, type='n')\n",
    "\n",
    "bus1 = get_element_index(net, \"bus\", \"Bus HV1\")\n",
    "\n",
    "\n",
    "# show bustable\n",
    "net.bus"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Lines\n",
    "\n",
    "<img src=\"pics/example_network_lines_hv.png\" width=\"40%\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The information about the 6 HV lines are stored in a csv file that we load from the hard drive:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:22.865299Z",
     "start_time": "2025-10-17T09:01:22.854137Z"
    }
   },
   "outputs": [],
   "source": [
    "hv_lines = pd.read_csv('example_advanced/hv_lines.csv', sep=';', header=0, decimal=',')\n",
    "hv_lines"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and use to create all lines:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:23.164355Z",
     "start_time": "2025-10-17T09:01:23.124315Z"
    }
   },
   "outputs": [],
   "source": [
    "# create lines\n",
    "for _, hv_line in hv_lines.iterrows():\n",
    "        from_bus = get_element_index(net, \"bus\", hv_line.from_bus)\n",
    "        to_bus = get_element_index(net, \"bus\", hv_line.to_bus)\n",
    "        create_line(net, from_bus, to_bus, length_km=hv_line.length,std_type=hv_line.std_type, name=hv_line.line_name, parallel=hv_line.parallel)\n",
    "\n",
    "# show line table\n",
    "net.line"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Transformer\n",
    "\n",
    "<img src=\"pics/example_network_trafos_hv.png\" width=\"40%\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The 380/110 kV transformer connects the buses \"Bus DB 1\" and \"Bus DB 2\". We use the get_element_index function from the pandapower toolbox to find the bus indices of the buses with these names and create a transformer by directly specifying the parameters:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:23.272168Z",
     "start_time": "2025-10-17T09:01:23.249758Z"
    }
   },
   "outputs": [],
   "source": [
    "hv_bus = get_element_index(net, \"bus\", \"Bus DB 2\")\n",
    "lv_bus = get_element_index(net, \"bus\", \"Bus SB 1\")\n",
    "create_transformer_from_parameters(net, hv_bus, lv_bus, sn_mva=300, vn_hv_kv=380, vn_lv_kv=110, vkr_percent=0.06,\n",
    "                                      vk_percent=8, pfe_kw=0, i0_percent=0, tp_pos=0, shift_degree=0, name='EHV-HV-Trafo')\n",
    "\n",
    "net.trafo # show trafo table"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Switches\n",
    "\n",
    "<img src=\"pics/example_network_switches_hv.png\" width=\"60%\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we create the switches to connect the buses in the transformer station. The switch configuration is stored in the following csv table:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:23.533149Z",
     "start_time": "2025-10-17T09:01:23.506876Z"
    }
   },
   "outputs": [],
   "source": [
    "hv_bus_sw = pd.read_csv('example_advanced/hv_bus_sw.csv', sep=';', header=0, decimal=',')\n",
    "hv_bus_sw"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:23.958678Z",
     "start_time": "2025-10-17T09:01:23.784602Z"
    }
   },
   "outputs": [],
   "source": [
    "# Bus-bus switches\n",
    "for _, switch in hv_bus_sw.iterrows():\n",
    "    from_bus = get_element_index(net, \"bus\", switch.from_bus)\n",
    "    to_bus = get_element_index(net, \"bus\", switch.to_bus)\n",
    "    create_switch(net, from_bus, to_bus, et=switch.et, closed=switch.closed, type=switch.type, name=switch.bus_name)\n",
    "\n",
    "# Bus-line switches\n",
    "hv_buses = net.bus[(net.bus.vn_kv == 380) | (net.bus.vn_kv == 110)].index\n",
    "hv_ls = net.line[(net.line.from_bus.isin(hv_buses)) & (net.line.to_bus.isin(hv_buses))]\n",
    "for _, line in hv_ls.iterrows():\n",
    "        create_switch(net, line.from_bus, line.name, et='l', closed=True, type='LBS', name=f\"Switch {net.bus.name.at[line.from_bus]} - {line['name']}\")\n",
    "        create_switch(net, line.to_bus, line.name, et='l', closed=True, type='LBS', name=f\"Switch {net.bus.name.at[line.to_bus]} - {line['name']}\")\n",
    "\n",
    "# Trafo-line switches\n",
    "create_switch(net, get_element_index(net, \"bus\", 'Bus DB 2'), get_element_index(net, \"trafo\", 'EHV-HV-Trafo'), et='t', closed=True, type='LBS', name='Switch DB2 - EHV-HV-Trafo')\n",
    "create_switch(net, get_element_index(net, \"bus\", 'Bus SB 1'), get_element_index(net, \"trafo\", 'EHV-HV-Trafo'), et='t', closed=True, type='LBS', name='Switch SB1 - EHV-HV-Trafo')\n",
    "\n",
    "# show switch table\n",
    "net.switch"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### External Grid\n",
    "\n",
    "<img src=\"pics/example_network_ext_grids_hv.png\" width=\"40%\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We equip the high voltage side of the transformer with an external grid connection:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:24.175638Z",
     "start_time": "2025-10-17T09:01:24.150059Z"
    }
   },
   "outputs": [],
   "source": [
    "create_ext_grid(net, get_element_index(net, \"bus\", 'Double Busbar 1'), vm_pu=1.03, va_degree=0, name='External grid',\n",
    "                   s_sc_max_mva=10000, rx_max=0.1, rx_min=0.1)\n",
    "\n",
    "net.ext_grid # show external grid table"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Loads\n",
    "\n",
    "<img src=\"pics/example_network_loads_hv.png\" width=\"40%\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The five loads in the HV network are defined in the following csv file:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:24.359877Z",
     "start_time": "2025-10-17T09:01:24.348468Z"
    }
   },
   "outputs": [],
   "source": [
    "hv_loads = pd.read_csv('example_advanced/hv_loads.csv', sep=';', header=0, decimal=',')\n",
    "hv_loads"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:24.729520Z",
     "start_time": "2025-10-17T09:01:24.695038Z"
    }
   },
   "outputs": [],
   "source": [
    "for _, load in hv_loads.iterrows():\n",
    "    bus_idx = get_element_index(net, \"bus\", load.bus)\n",
    "    create_load(net, bus_idx, p_mw=load.p, q_mvar=load.q, name=load.load_name)\n",
    "\n",
    "# show load table\n",
    "net.load"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Generator\n",
    "\n",
    "<img src=\"pics/example_network_gens_hv.png\" width=\"40%\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The voltage controlled generator is created with an active power of 100 MW (negative for generation) and a voltage set point of 1.03 per unit:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:24.884238Z",
     "start_time": "2025-10-17T09:01:24.855726Z"
    }
   },
   "outputs": [],
   "source": [
    "create_gen(net, get_element_index(net, \"bus\", 'Bus HV4'), vm_pu=1.03, p_mw=100, name='Gas turbine')\n",
    "\n",
    "# show generator table\n",
    "net.gen"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Static generators\n",
    "\n",
    "<img src=\"pics/example_network_sgens_hv.png\" width=\"40%\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We create this wind park with an active power of 20 MW (negative for generation) and a reactive power of -4 Mvar. To classify the generation as a wind park, we set type to \"WP\":"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:25.292770Z",
     "start_time": "2025-10-17T09:01:25.280564Z"
    }
   },
   "outputs": [],
   "source": [
    "create_sgen(net, get_element_index(net, \"bus\", 'Bus SB 5'), p_mw=20, q_mvar=4, sn_mva=45, \n",
    "               type='WP', name='Wind Park')\n",
    "\n",
    "# show static generator table\n",
    "net.sgen"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Shunt\n",
    "\n",
    "<img src=\"pics/example_network_shunts_hv.png\" width=\"40%\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:25.610351Z",
     "start_time": "2025-10-17T09:01:25.581931Z"
    }
   },
   "outputs": [],
   "source": [
    "create_shunt(net, bus1, p_mw=0, q_mvar=0.960, name='Shunt')\n",
    "\n",
    "# show shunt table\n",
    "net.shunt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### External network equivalents\n",
    "\n",
    "\n",
    "<img src=\"pics/example_network_ext_equi_hv.png\" width=\"40%\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The two remaining elements are impedances and extended ward equivalents:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:26.063788Z",
     "start_time": "2025-10-17T09:01:26.035634Z"
    }
   },
   "outputs": [],
   "source": [
    "# Impedance\n",
    "create_impedance(net, get_element_index(net, \"bus\", 'Bus HV3'), bus1,\n",
    "                    rft_pu=0.074873, xft_pu=0.198872, sn_mva=100, name='Impedance')\n",
    "\n",
    "# show impedance table\n",
    "net.impedance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:26.453212Z",
     "start_time": "2025-10-17T09:01:26.411436Z"
    }
   },
   "outputs": [],
   "source": [
    "# xwards\n",
    "create_xward(net, get_element_index(net, \"bus\", 'Bus HV3'), ps_mw=23.942, qs_mvar=-12.24187, pz_mw=2.814571, \n",
    "                qz_mvar=0, r_ohm=0, x_ohm=12.18951, vm_pu=1.02616, name='XWard 1')\n",
    "create_xward(net, bus1, ps_mw=3.776, qs_mvar=-7.769979, pz_mw=9.174917,\n",
    "                qz_mvar=0, r_ohm=0, x_ohm=50.56217, vm_pu=1.024001, name='XWard 2')\n",
    "\n",
    "# show xward table\n",
    "net.xward"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Medium voltage level"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Buses\n",
    "\n",
    "<img src=\"pics/example_network_buses_mv.png\" width=\"50%\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:26.981691Z",
     "start_time": "2025-10-17T09:01:26.926137Z"
    }
   },
   "outputs": [],
   "source": [
    "create_bus(net, name='Bus MV0 20kV', vn_kv=20, type='n')\n",
    "for i in range(8):\n",
    "    create_bus(net, name='Bus MV%s' % i, vn_kv=10, type='n')\n",
    "\n",
    "#show only medium voltage bus table\n",
    "mv_buses = net.bus[(net.bus.vn_kv == 10) | (net.bus.vn_kv == 20)]\n",
    "mv_buses"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Lines\n",
    "\n",
    "<img src=\"pics/example_network_lines_mv.png\" width=\"50%\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:27.372825Z",
     "start_time": "2025-10-17T09:01:27.272397Z"
    }
   },
   "outputs": [],
   "source": [
    "mv_lines = pd.read_csv('example_advanced/mv_lines.csv', sep=';', header=0, decimal=',')\n",
    "for _, mv_line in mv_lines.iterrows():\n",
    "    from_bus = get_element_index(net, \"bus\", mv_line.from_bus)\n",
    "    to_bus = get_element_index(net, \"bus\", mv_line.to_bus)\n",
    "    create_line(net, from_bus, to_bus, length_km=mv_line.length, std_type=mv_line.std_type, name=mv_line.line_name)\n",
    "\n",
    "# show only medium voltage lines\n",
    "net.line[net.line.from_bus.isin(mv_buses.index)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3 Winding Transformer\n",
    "\n",
    "<img src=\"pics/example_network_trafos_mv.png\" width=\"50%\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The three winding transformer transforms its high voltage level to two different lower voltage levels, in this case from 110 kV to 20 kV and 10 kV."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:27.998743Z",
     "start_time": "2025-10-17T09:01:27.940679Z"
    }
   },
   "outputs": [],
   "source": [
    "hv_bus = get_element_index(net, \"bus\", \"Bus HV2\")\n",
    "mv_bus = get_element_index(net, \"bus\", \"Bus MV0 20kV\")\n",
    "lv_bus = get_element_index(net, \"bus\", \"Bus MV0\")\n",
    "create_transformer3w_from_parameters(net, hv_bus, mv_bus, lv_bus, vn_hv_kv=110, vn_mv_kv=20, vn_lv_kv=10, \n",
    "                                        sn_hv_mva=40, sn_mv_mva=15, sn_lv_mva=25, vk_hv_percent=10.1, \n",
    "                                        vk_mv_percent=10.1, vk_lv_percent=10.1, vkr_hv_percent=0.266667, \n",
    "                                        vkr_mv_percent=0.033333, vkr_lv_percent=0.04, pfe_kw=0, i0_percent=0, \n",
    "                                        shift_mv_degree=30, shift_lv_degree=30, tap_side=\"hv\", tap_neutral=0, tap_min=-8, \n",
    "                                        tap_max=8, tap_step_percent=1.25, tap_pos=0, name='HV-MV-MV-Trafo', \n",
    "                                        tap_changer_type=\"Ratio\")\n",
    "\n",
    "# show transformer3w table\n",
    "net.trafo3w"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Switches"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:28.452970Z",
     "start_time": "2025-10-17T09:01:28.330900Z"
    }
   },
   "outputs": [],
   "source": [
    "# Bus-line switches\n",
    "mv_buses = net.bus[(net.bus.vn_kv == 10) | (net.bus.vn_kv == 20)].index\n",
    "mv_ls = net.line[(net.line.from_bus.isin(mv_buses)) & (net.line.to_bus.isin(mv_buses))]\n",
    "for _, line in mv_ls.iterrows():\n",
    "        create_switch(net, line.from_bus, line.name, et='l', closed=True, type='LBS', name=f\"Switch {net.bus.name.at[line.from_bus]} - {line['name']}\")\n",
    "        create_switch(net, line.to_bus, line.name, et='l', closed=True, type='LBS', name=f\"Switch {net.bus.name.at[line.to_bus]} - {line['name']}\")\n",
    "\n",
    "# open switch\n",
    "open_switch_id = net.switch[(net.switch.name == 'Switch Bus MV5 - MV Line5')].index\n",
    "net.switch.closed.loc[open_switch_id] = False\n",
    "\n",
    "#show only medium voltage switch table\n",
    "net.switch[net.switch.bus.isin(mv_buses)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Loads\n",
    "\n",
    "<img src=\"pics/example_network_loads_mv.png\"  width=\"50%\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:29.041400Z",
     "start_time": "2025-10-17T09:01:28.985473Z"
    }
   },
   "outputs": [],
   "source": [
    "mv_loads = pd.read_csv('example_advanced/mv_loads.csv', sep=';', header=0, decimal=',')\n",
    "for _, load in mv_loads.iterrows():\n",
    "    bus_idx = get_element_index(net, \"bus\", load.bus)\n",
    "    create_load(net, bus_idx, p_mw=load.p, q_mvar=load.q, name=load.load_name)\n",
    "\n",
    "# show only medium voltage loads\n",
    "net.load[net.load.bus.isin(mv_buses)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Static generators\n",
    "\n",
    "<img src=\"pics/example_network_sgens_mv.png\"  width=\"50%\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:29.267676Z",
     "start_time": "2025-10-17T09:01:29.225671Z"
    }
   },
   "outputs": [],
   "source": [
    "mv_sgens = pd.read_csv('example_advanced/mv_sgens.csv', sep=';', header=0, decimal=',')\n",
    "for _, sgen in mv_sgens.iterrows():\n",
    "    bus_idx = get_element_index(net, \"bus\", sgen.bus)\n",
    "    create_sgen(net, bus_idx, p_mw=sgen.p, q_mvar=sgen.q, sn_mva=sgen.sn, type=sgen.type, name=sgen.sgen_name)\n",
    "\n",
    "# show only medium voltage static generators\n",
    "net.sgen[net.sgen.bus.isin(mv_buses)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Low voltage level"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Busses\n",
    "\n",
    "<img src=\"pics/example_network_buses_lv.png\" width=\"50%\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:29.689687Z",
     "start_time": "2025-10-17T09:01:29.637735Z"
    }
   },
   "outputs": [],
   "source": [
    "create_bus(net, name='Bus LV0', vn_kv=0.4, type='n')\n",
    "for i in range(1, 6):\n",
    "    create_bus(net, name='Bus LV1.%s' % i, vn_kv=0.4, type='m')\n",
    "for i in range(1, 5):\n",
    "    create_bus(net, name='Bus LV2.%s' % i, vn_kv=0.4, type='m')\n",
    "create_bus(net, name='Bus LV2.2.1', vn_kv=0.4, type='m')\n",
    "create_bus(net, name='Bus LV2.2.2', vn_kv=0.4, type='m')\n",
    "\n",
    "# show only low voltage buses\n",
    "lv_buses = net.bus[net.bus.vn_kv == 0.4]\n",
    "lv_buses"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Lines\n",
    "\n",
    "<img src=\"pics/example_network_lines_lv.png\" width=\"50%\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:30.130260Z",
     "start_time": "2025-10-17T09:01:30.022708Z"
    }
   },
   "outputs": [],
   "source": [
    "# create lines\n",
    "lv_lines = pd.read_csv('example_advanced/lv_lines.csv', sep=';', header=0, decimal=',')\n",
    "for _, lv_line in lv_lines.iterrows():\n",
    "    from_bus = get_element_index(net, \"bus\", lv_line.from_bus)\n",
    "    to_bus = get_element_index(net, \"bus\", lv_line.to_bus)\n",
    "    create_line(net, from_bus, to_bus, length_km=lv_line.length, std_type=lv_line.std_type, name=lv_line.line_name)\n",
    "\n",
    "# show only low voltage lines\n",
    "net.line[net.line.from_bus.isin(lv_buses.index)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Transformer\n",
    "\n",
    "<img src=\"pics/example_network_trafos_lv.png\" width=\"50%\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:30.531511Z",
     "start_time": "2025-10-17T09:01:30.496953Z"
    }
   },
   "outputs": [],
   "source": [
    "hv_bus = get_element_index(net, \"bus\", \"Bus MV4\")\n",
    "lv_bus = get_element_index(net, \"bus\",\"Bus LV0\")\n",
    "create_transformer_from_parameters(net, hv_bus, lv_bus, sn_mva=.4, vn_hv_kv=10, vn_lv_kv=0.4, vkr_percent=1.325, vk_percent=4, pfe_kw=0.95, i0_percent=0.2375, tap_side=\"hv\", tap_neutral=0, tap_min=-2, tap_max=2, tap_step_percent=2.5, tap_changer_type=\"Ratio\", tp_pos=0, shift_degree=150, name='MV-LV-Trafo')\n",
    "\n",
    "#show only low voltage transformer\n",
    "net.trafo[net.trafo.lv_bus.isin(lv_buses.index)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Switches"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:30.798023Z",
     "start_time": "2025-10-17T09:01:30.716122Z"
    }
   },
   "outputs": [],
   "source": [
    "lv_buses\n",
    "# Bus-line switches\n",
    "lv_ls = net.line[(net.line.from_bus.isin(lv_buses.index)) & (net.line.to_bus.isin(lv_buses.index))]\n",
    "for _, line in lv_ls.iterrows():\n",
    "        create_switch(net, line.from_bus, line.name, et='l', closed=True, type='LBS', name=f\"Switch {net.bus.name.at[line.from_bus]} - {line['name']}\")\n",
    "        create_switch(net, line.to_bus, line.name, et='l', closed=True, type='LBS', name=f\"Switch {net.bus.name.at[line.to_bus]} - {line['name']}\")\n",
    "\n",
    "# Trafo-line switches\n",
    "create_switch(net, get_element_index(net, \"bus\", 'Bus MV4'), get_element_index(net, \"trafo\", 'MV-LV-Trafo'), et='t', closed=True, type='LBS', name='Switch MV4 - MV-LV-Trafo')\n",
    "create_switch(net, get_element_index(net, \"bus\", 'Bus LV0'), get_element_index(net, \"trafo\", 'MV-LV-Trafo'), et='t', closed=True, type='LBS', name='Switch LV0 - MV-LV-Trafo')\n",
    "\n",
    "# show only low vvoltage switches\n",
    "net.switch[net.switch.bus.isin(lv_buses.index)]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Loads\n",
    "\n",
    "<img src=\"pics/example_network_loads_lv.png\" width=\"50%\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:31.363008Z",
     "start_time": "2025-10-17T09:01:31.306096Z"
    }
   },
   "outputs": [],
   "source": [
    "lv_loads = pd.read_csv('example_advanced/lv_loads.csv', sep=';', header=0, decimal=',')\n",
    "for _, load in lv_loads.iterrows():\n",
    "    bus_idx = get_element_index(net, \"bus\", load.bus)\n",
    "    create_load(net, bus_idx, p_mw=load.p, q_mvar=load.q, name=load.load_name)\n",
    "    \n",
    "# show only low voltage loads\n",
    "net.load[net.load.bus.isin(lv_buses.index)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Static generators\n",
    "\n",
    "<img src=\"pics/example_network_sgens_lv.png\"  width=\"50%\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:31.674575Z",
     "start_time": "2025-10-17T09:01:31.619691Z"
    }
   },
   "outputs": [],
   "source": [
    "lv_sgens = pd.read_csv('example_advanced/lv_sgens.csv', sep=';', header=0, decimal=',')\n",
    "for _, sgen in lv_sgens.iterrows():\n",
    "    bus_idx = get_element_index(net, \"bus\", sgen.bus)\n",
    "    create_sgen(net, bus_idx, p_mw=sgen.p, q_mvar=sgen.q, sn_mva=sgen.sn, type=sgen.type, name=sgen.sgen_name)\n",
    "\n",
    "# show only low voltage static generators\n",
    "net.sgen[net.sgen.bus.isin(lv_buses.index)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Run a Power Flow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-17T09:01:35.906398Z",
     "start_time": "2025-10-17T09:01:32.163847Z"
    }
   },
   "outputs": [],
   "source": [
    "runpp(net, calculate_voltage_angles=True, init=\"dc\")\n",
    "net"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
